package chapter24

/**
 * Created by alex on 15-4-16.
 */
object EMail {

  //注入方法(可选的)
  def apply(user: String, domain: String) = user + "@" + domain

  //抽取方法(规定的)
  def unapply(str: String) : Option[(String,String)] = {
    val parts = str split "@"
    if (parts.length == 2) Some(parts(0),parts(1)) else None
  }

  def aa(s: String):Any =
    s match {
      case EMail(user,domain) => println(user + "++" + domain)
      case _                  => println("none...")
    }
}

/**
 * 匹配一个字符串
 */
object Twice {

  def apply(s: String): String = s + s

  def unapply(s: String): Option[String] = {

   // println("Twice____: " + s)

    val length = s.length / 2
    val half = s.substring(0,length)
    println(s.substring(length))
    if(half == s.substring(length)) Some(half) else None
  }
}

object UpperCase {
  def unapply(s: String): Boolean = s.toUpperCase == s
}

object Run extends App{

  def userTwiceUpper(s: String) = s match {
    case EMail(Twice(x @ UpperCase()),domain) => "okkkkk___: " + x
    case _                                    => "not ok"
  }

  println( userTwiceUpper("DIDI@163.com"))
}